import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/widgets/gift/model/xindong_gift_count_class.dart';
import 'package:xindong/widgets/gift/xindong_songli_controller.dart';

class XinDongSongLiCountDialog extends StatefulWidget {
  // static const List<int> numberList = [3344, 1314, 520, 66, 10, 1];// 接口数据代替
  // 唯一标识
  static const String tag = "kTagGiftSendCount";

  const XinDongSongLiCountDialog({Key? key}) : super(key: key);

  XinDongSongLiCountDialog.show(BuildContext context, {super.key, double spaceHeight = 0}) {
    SmartDialog.showAttach(
      tag: tag,
      alignment: Alignment.topCenter,
      maskColor: Colors.transparent,
      clickMaskDismiss: true,
      builder: (BuildContext context) => this,
      targetContext: context,
    );
  }

  static hide() {
    try {
      SmartDialog.dismiss(status: SmartStatus.attach, tag: tag);
    } catch (error) {
      mCatchPrint(error);
    }
  }

  @override
  State<XinDongSongLiCountDialog> createState() => _XinDongSongLiCountDialogState();
}

class _XinDongSongLiCountDialogState extends State<XinDongSongLiCountDialog> {
  static const String allIn = "All in";
  static const double itemHeight = 32.0;
  final TextStyle countStyle = const TextStyle(fontSize: 14, color: DZColors.mainBlueColor, height: 1.1);
  final TextStyle textStyle = const TextStyle(fontSize: 14, color: DZColors.placeholderColor, height: 1.1);

  var numList = [
    {"num": 1, "name": '一心一意'},
    {"num": 10, "name": '十全十美'},
    {"num": 30, "name": '想你...'},
    {"num": 66, "name": '一切顺利'},
    {"num": 188, "name": '要抱抱'},
    {"num": 520, "name": '我爱你'},
    {"num": 1314, "name": '一生一世'},
    {"num": 3344, "name": '生生世世'}
  ];

  @override
  Widget build(BuildContext context) {
    List dataList = [];
    // 背包礼物可以全部赠送
    final selectGift = XinDongSongLiController.to.selectGift;
    if (selectGift?.cate_id == XinDongLiWuType.backpack) {
      dataList.add(allIn);
    }
    dataList.addAll(numList.map((e) => XinDongGiftCountClass.fromJson(e)).toList());
    return Container(
      width: 130,
      margin: const EdgeInsets.only(bottom: 16),
      decoration: BoxDecoration(color: DZColors.white, borderRadius: BorderRadius.circular(6.0)),
      child: MediaQuery.removePadding(
        context: context,
        removeTop: true,
        child: ListView.builder(
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          padding: const EdgeInsets.only(top: 5, bottom: 5),
          itemCount: dataList.length,
          itemBuilder: (BuildContext context, int index) {
            var object = dataList[index];
            if (object == allIn) {
              // All in
              return GestureDetector(
                onTap: () {
                  XinDongSongLiController.to.onSendCountChanged(selectGift?.total ?? 1);
                  XinDongSongLiCountDialog.hide();
                },
                behavior: HitTestBehavior.opaque,
                child: Container(
                  height: itemHeight,
                  alignment: Alignment.center,
                  child: Text(object, style: countStyle),
                ),
              );
            } else {
              XinDongGiftCountClass model = object as XinDongGiftCountClass;
              return GestureDetector(
                onTap: () {
                  XinDongSongLiController.to.onSendCountChanged(model.num);
                  XinDongSongLiCountDialog.hide();
                },
                behavior: HitTestBehavior.opaque,
                child: Container(
                  height: itemHeight,
                  alignment: Alignment.center,
                  padding: const EdgeInsets.only(left: 15, right: 15),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text("${model.num}", style: countStyle),
                      Text(model.name, style: textStyle),
                    ],
                  ),
                ),
              );
            }
          },
        ),
      ),
    );
  }
}
